package cn.com.java.controller;

import cn.com.java.core.HttpRes;
import cn.com.java.model.SysRole;
import cn.com.java.service.SysRoleMenuService;
import cn.com.java.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("SysRole")
@RequiredArgsConstructor
public class SysRoleController {


    private final SysRoleService sysRoleService;

    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public List<SysRole> listAll() {
        return sysRoleService.list();
    }

    @GetMapping("listByPage")
    public HttpRes listByPage(int pageNum, int pageSize, String roleName) {
        Page<SysRole> page = new Page<>(pageNum, pageSize);

        QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
        if (roleName != null && !roleName.isEmpty()) {
            sysRoleQueryWrapper.like("role_name", roleName);
        }
        return HttpRes.OK("查询成功", sysRoleService.page(page, sysRoleQueryWrapper));
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public SysRole getById(Long id) {
        return sysRoleService.getById(id);
    }

    /**
     * 新增，忽略null字段
     *
     * @param sysRole 新增的记录
     * @return 返回影响行数
     */
    @RequestMapping("insert")
    public HttpRes insert(@Valid @RequestBody SysRole sysRole) {
        sysRoleMenuService.insertBySysRole(sysRole);
        return HttpRes.OK("添加成功", sysRoleService.getBaseMapper().insert(sysRole));
    }

    /**
     * 修改，忽略null字段
     *
     * @param sysRole 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public HttpRes update(@Valid @RequestBody SysRole sysRole) {
        sysRoleMenuService.updateBySysRole(sysRole);
        return HttpRes.OK("修改成功", sysRoleService.updateById(sysRole));
    }

    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    @DeleteMapping("delete")
    public HttpRes delete(Long id) {
        return HttpRes.OK("删除成功",sysRoleService.removeById(id));
    }

}